//
//  LJNewFeatureViewController.swift
//  LJWeibo
//
//  Created by Raymond on 15/12/17.
//  Copyright © 2015年 liu. All rights reserved.
//

import UIKit

private let reuseIdentifier = "LJNewFeatureCell"

class LJNewFeatureViewController: UICollectionViewController {
    /// item数量
    private let itemCount = 4
    /// layout属性
    private var layout = UICollectionViewFlowLayout()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.perpareCollectionView()

    }
    /// 构造方法
    init(){
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 准备collectionview
    private func perpareCollectionView(){
        // 注册cell
        self.collectionView!.registerClass(LJNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        // 设置item大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        // 设置间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        // 设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        // 设置分页
        collectionView?.pagingEnabled = true
        // 关闭弹簧效果
        collectionView?.bounces = false
        
        
    }


    //MARK: - collectionViewContoller数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return itemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! LJNewFeatureCell
        
        // Configure the cell
        cell.index = indexPath.row + 1
    
        return cell
    }
    
    //MARK: - collectionView代理方法
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //获取当前正在显示的item数
        let indexPath = collectionView.indexPathsForVisibleItems().last
        let index = indexPath?.item
        // 根据index获得对应cell
        let cell = collectionView.cellForItemAtIndexPath(indexPath!) as! LJNewFeatureCell
        // 最后一页显示按钮
        if index == itemCount - 1{
           cell.startAnimation()
        }
        
        
    }
    
    
    //MARK: - 自定义collectionViewCell
    class LJNewFeatureCell: UICollectionViewCell {
        var index = 0{
            didSet{
                //赋值，改变背景图片
                bkgImageView.image = UIImage(named: "new_feature_\(index)")
                
                //隐藏按钮
                startButton.hidden = true
            }
        }
        override init(frame: CGRect) {
            super.init(frame: frame)
            // 准备UI
            self.prepareUI()
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        // 开始按钮点击时调用的方法
        func startButtonDidClick(){
            (UIApplication.sharedApplication().delegate as! AppDelegate).swtichRootViewController(true)
        }
        
        // 按钮动画开始方法
        func startAnimation(){
            // 显示按钮
            startButton.hidden = false
            
            startButton.transform = CGAffineTransformMakeScale(0, 0)
            
            UIView .animateWithDuration(0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                self.startButton.transform = CGAffineTransformIdentity
                }) { (_) -> Void in
                    print("动画完成")
            }
        }
        /// 准备UI
        private func prepareUI(){
            // 添加背景图片
            contentView.addSubview(bkgImageView)
            //添加按钮
            contentView.addSubview(startButton)
            
            //添加约束
            bkgImageView.ff_Fill(contentView)
            
            startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPointMake(0, -160))
        }
        
        //MARK: - 懒加载
        // 背景图
        private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
        
        // 按钮
        private lazy var startButton: UIButton = {
            let button = UIButton()
            button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
            button.setTitle("立即体验", forState: UIControlState.Normal)
            
            button.addTarget(self, action: "startButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
            return button
        }()
    }

    
}
